package com.cn.mq.produce;

import java.util.UUID;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cn.mq.config.AmqpConfig;
import com.cn.mq.message.Message;

/**
 * @ClassName: Produce
 * @Description: TODO
 * @author Administrator
 * @date 2017年7月4日
 */
@Component
public class TopicProduce implements RabbitTemplate.ConfirmCallback {
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	public void sender(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
		this.rabbitTemplate.setConfirmCallback(this);
	}
	
	public void send(Message msg) {
		System.out.println("发送消息:"+msg.getId());
		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
		this.rabbitTemplate.convertAndSend("topicExchange",
				AmqpConfig.QUEUE_TOP_IC_A, msg, correlationData);
	}

	// 回调
	@Override
	public void confirm(CorrelationData correlationData, boolean ack,String cause) {

		System.out.println("回调信息>>id:"+correlationData.getId() + ">>ack:"+ack + ">>cause:"+ cause);
	}
}
